/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */

package src.clovece.game;


import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class used to save settings and games
 */
public class Save {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    /**
     * Saves actual settings to XML file
     */
    public Save()
    {
        Properties settings = new Properties();

        settings.put("GameSet", ""+ gameInfo.getGraphicMode());
        settings.put("Host", gameInfo.getHost());
        settings.put("NumberOfPlayers",""+ gameInfo.getNumberOfPlayers());
        settings.put("GameBoardSize",""+ gameInfo.getGameBoardSize());
        settings.put("Username",gameInfo.getPlayerNickname(0));
        settings.put("ChosenFigurine",""+ gameInfo.getChosenFigurine());


        try {
             settings.storeToXML(new FileOutputStream(System.getProperty("user.dir") + System.getProperty("file.separator") + "lib" +
                   System.getProperty("file.separator") + "settings.xml"), null,"utf-8");
        }
        catch (IOException e) {
        }
    }

    /**
     * Saves actual game to properties file
     */
    public void SaveGame(String savename)
    {
        Properties save = new Properties();


        save.put("PlayerNickName", gameInfo.getPlayerNickname(0));
        save.put("GraphicMode", ""+gameInfo.getGraphicMode());
        save.put("ChosenFigurine", ""+gameInfo.getChosenFigurine());
        save.put("Die", ""+gameInfo.getDie());
        save.put("PlayerOnTurn", ""+gameInfo.getPlayerOnTurn());
        save.put("GameBoardSize", ""+gameInfo.getGameBoardSize());
        save.put("NumberOfPlayers", ""+gameInfo.getNumberOfPlayers());
        save.put("GeneratedValue0", ""+gameInfo.getGeneratedValues()[0]);
        save.put("GeneratedValue1", ""+gameInfo.getGeneratedValues()[1]);
        save.put("GeneratedValue2", ""+gameInfo.getGeneratedValues()[2]);
        save.put("GeneratedValue3", ""+gameInfo.getGeneratedValues()[3]);
        save.put("GeneratedValue4", ""+gameInfo.getGeneratedValues()[4]);
        save.put("GeneratedValue5", ""+gameInfo.getGeneratedValues()[5]);
        
        if (gameInfo.getGamePhase() == true)
            save.put("GamePhase", "true");
        else save.put("GamePhase", "false");
        
        //player on index 0
        save.put("Player0-position", ""+gameInfo.getPlayers()[0].getPosition());
        
        //player on index 1
        save.put("Player1-position", ""+gameInfo.getPlayers()[1].getPosition());
        AiPlayer cpu = (AiPlayer) gameInfo.getPlayers()[1];

        if (cpu.getRandomMove() == true)
            save.put("Player1-RandomMove", "true");
        else save.put("Player1-RandomMove", "false");

        if (cpu.getPreferHome() == true)
            save.put("Player1-PreferHome", "true");
        else save.put("Player1-PreferHome", "false");

        if (cpu.getPreferNew() == true)
            save.put("Player1-PreferNew", "true");
        else save.put("Player1-PreferNew", "false");

        //player on index 2
        if (gameInfo.getNumberOfPlayers() > 2)
        {
            save.put("Player2-position", ""+gameInfo.getPlayers()[2].getPosition());
            cpu = (AiPlayer) gameInfo.getPlayers()[2];

            if (cpu.getRandomMove() == true)
                save.put("Player2-RandomMove", "true");
            else save.put("Player2-RandomMove", "false");

            if (cpu.getPreferHome() == true)
                save.put("Player2-PreferHome", "true");
            else save.put("Player2-PreferHome", "false");

            if (cpu.getPreferNew() == true)
                save.put("Player2-PreferNew", "true");
            else save.put("Player2-PreferNew", "false");

        }
        
        //player on index 3
        if (gameInfo.getNumberOfPlayers() > 3)
        {
            save.put("Player3-position", ""+gameInfo.getPlayers()[3].getPosition());
            cpu = (AiPlayer) gameInfo.getPlayers()[3];

            if (cpu.getRandomMove() == true)
                save.put("Player3-RandomMove", "true");
            else save.put("Player3-RandomMove", "false");

            if (cpu.getPreferHome() == true)
                save.put("Player3-PreferHome", "true");
            else save.put("Player3-PreferHome", "false");

            if (cpu.getPreferNew() == true)
                save.put("Player3-PreferNew", "true");
            else save.put("Player3-PreferNew", "false");

        }

        for (int i = 0; i < gameInfo.getNumberOfPlayers(); i++)
        {

            for (int j = 0; j < 4; j++)
            {
                save.put("Player"+i+"Figurine"+j+"-ActualPosition", ""+ gameInfo.getPlayers()[i].figurines[j].getActualPosition());
                save.put("Player"+i+"Figurine"+j+"-StartingPosition", ""+ gameInfo.getPlayers()[i].figurines[j].getStartingPosition());
                save.put("Player"+i+"Figurine"+j+"-Number", ""+ gameInfo.getPlayers()[i].figurines[j].getNumber());
                save.put("Player"+i+"Figurine"+j+"-Counter", ""+ gameInfo.getPlayers()[i].figurines[j].getCounter());
                save.put("Player"+i+"Figurine"+j+"-Figurine", ""+ gameInfo.getPlayers()[i].figurines[j].getFigurine());
                save.put("Player"+i+"Figurine"+j+"-BelongsTo", ""+ i);

                if (gameInfo.getPlayers()[i].figurines[j].getGameCompleted() == true)
                    save.put("Player"+i+"Figurine"+j+"-GameCompleted", "true");
                else save.put("Player"+i+"Figurine"+j+"-GameCompleted", "false");

            }


        }
       // gameInfo.getPlayers()[2].figurines[4].getPlayer().getPosition(); //uloz index ak





        try {
            save.store(new FileOutputStream(System.getProperty("user.dir") + System.getProperty("file.separator") + "examples" +
                   System.getProperty("file.separator")  + "save" + System.getProperty("file.separator") + savename + ".svg"), null);

        }
        catch (IOException e) {
        }


    }

}
